package com.jade.zsy.base.facade.impl;

import com.eglsc.tools.util.ExcelUtil;
import com.jade.bss.DbException;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.base.utils.MsgUtil;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.context.ApplicationContextUtils;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.zsy.base.common.StatusConstant;
import com.jade.zsy.base.dao.WarehouseDao;
import com.jade.zsy.base.entity.BusinessCode;
import com.jade.zsy.base.entity.Warehouse;
import com.jade.zsy.base.facade.BusinessCodeFacade;
import com.jade.zsy.base.facade.WarehouseFacade;
import com.jade.zsy.base.vo.WarehouseVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/6/3
 * Time: 12:59
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
@Service("WarehouseFacade")
public class WarehouseFacadeImpl extends BaseFacadeImpl implements WarehouseFacade {
    private BusinessCodeFacade businessCodeFacade = ApplicationContextUtils.getBean("BusinessCodeFacade");
    @Inject
    @Named("WarehouseDao")
    private WarehouseDao entityDao;
    @Override
    @Transactional
    public void save(Object obj) throws DbException {
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        Warehouse entity=(Warehouse)obj;
        if(StringUtils.isEmpty(entity.getCode()))//用户自定义为空系统自动生成
            entity.setCode(businessCodeFacade.nextBusinessCode(BusinessCode.KEY_BASE_WAREHOUSE));
        entity.setOwnerId(customer.getOwnerId());
        entity.setCreator(customer.getNickName());
        entity.setCreatorId(customer.getId());
        entity.setStatus(StatusConstant.RECORD_NORMAL);
        super.save(entity);
    }
    @Override
    @Transactional
    public void update(Object obj) throws DbException {
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        Warehouse entity=(Warehouse)obj;
        entity.setModifier(customer.getNickName());
        entity.setModifyTime(new Date());
        super.update(entity);
    }
    @Override
    @Transactional
    public void delete(long... obj) throws DbException {
        for(long id : obj){
            Warehouse org=entityDao.getById(Warehouse.class,id);
            if(org==null || org.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            entityDao.delete(Warehouse.class,org);
        }
    }
    @Override
    @Transactional
    public void disabled(long... obj) throws DbException {
        for(long id : obj){
            Warehouse org=entityDao.getById(Warehouse.class,id);
            if(org==null || org.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            org.setStatus(StatusConstant.RECORD_STOP);
            entityDao.update(org);
        }
    }
    @Override
    @Transactional
    public void enabled(long... obj) throws DbException {
        for(long id : obj){
            Warehouse org=entityDao.getById(Warehouse.class,id);
            if(org==null || org.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            org.setStatus(StatusConstant.RECORD_NORMAL);
            entityDao.update(org);
        }
    }
    @Override
    @Transactional
    public void importEntity(InputStream is) throws DbException {
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        Map<String, String> headerMap = new HashMap<String, String>();
        headerMap.put("code", "员工编号");
        headerMap.put("name", "姓名");
        headerMap.put("gender", "性别");
        headerMap.put("phone", "手机号码");
        headerMap.put("tel", "电话");
        headerMap.put("email", "邮箱");
        headerMap.put("birth", "出生日期");
        headerMap.put("certNo", "证件号码");
        ExcelUtil excelUtil = new ExcelUtil();
        List<Warehouse> dataList = excelUtil.readExcelByIO(headerMap, is, Warehouse.class, 10000);
        for (Warehouse employee : dataList){
            employee.setOwnerId(customer.getOwnerId());
            employee.setCreator(customer.getNickName());
            employee.setCreatorId(customer.getId());
        }
        super.batchSave(dataList);
    }
    @Transactional
    public void exportEntity() throws DbException {
        ExcelUtil excelUtil=new ExcelUtil();

    }
    @Override
    public PagingResult queryEntity(Object obj, int page, int size, String keys, String... orders) throws DbException,IllegalAccessException, InstantiationException {
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        WarehouseVo entity=(WarehouseVo)obj;
        Criteria criteria = new Criteria((page - 1) * size, size);
        criteria.add(Restrictions.eq("ownerId",customer.getOwnerId()));
        if(StringUtils.isNotEmpty(keys)){
            criteria.add(Restrictions.or(Restrictions.like("phone",keys), Restrictions.eq("name",keys)));
        }
        if(entity!=null && StringUtils.isNotEmpty(entity.getPhone())){
            criteria.add(Restrictions.like("phone",entity.getPhone()));
        }
        if(entity!=null && StringUtils.isNotEmpty(entity.getName())){
            criteria.add(Restrictions.like("name",entity.getName()));
        }
        if(entity!=null &&entity.getStatus()>0){
            criteria.add(Restrictions.eq("status",entity.getStatus()));
        }

        PagingList pagingList = entityDao.queryEntity(Warehouse.class, criteria);
        PagingResult result = PagingResult.getResult(CopyBean.copy(pagingList, WarehouseVo.class), pagingList.getTotalCount(), page, size);
        return result;
    }
}
